<?php

namespace app\api\model;

use think\Model;

class Batch extends Model
{

    protected $add_rule =   [
        'product_id'   => 'require',
        'code'   => 'require',
        'company_id'       =>'require',
        'number'   => 'require',
//        'grade_id'       =>'require',
    ];

    protected $add_message  =   [
        'product_id.require' => '所属产品不能为空',
        'code.require' => '产品批号并不能为空',
        'company_id.require' => '所属企业不能为空',
//        'grade_id.require' => '请选择产品等级',
        'number.require' => '批次数量不能为空',
    ];

    public function add_vali($data)
    {





        try {
            validate($this->add_rule,$this->add_message)->check($data);
            if($data['wdm_open']  && $data['wdm_end']){
                $info = $this->where('code',$data['code'])->where('status',1)->find();
                if($info){
                    return re_error('已存在该批号');
                }
                if($data['wdm_open'] > $data['wdm_end']){
                    return re_error('微点码区间不正确');
                }
                //如果开始编号小于已经存在的，则不可录入
                $wdm_open_info = $this->where('wdm_end','>',$data['wdm_open'])->whereOr('wdm_end','=',$data['wdm_open'])->find();

                if($wdm_open_info){
                    return re_error('微点码开始点'.$data['wdm_open'].'已被占用，请选用其他区间');
                }
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function add($data)
    {
        $data['time'] = date('Y-m-d H:i:s');
        $this->startTrans();
        try {
            $this->allowField([
                'name',
                'time',
                'user_id',
                'product_id',
                'traceability',
                'status',
                'content',
                'grade_id',
                'code',
                'number',
                'company_id',
                'wdm_end',
                'wdm_open'
            ])->save($data);
            $this->commit();
            return re_success("添加成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $edit_rule =   [
        'id'   => 'require',
        'product_id'   => 'require',
        'code'   => 'require',
        'company_id'       =>'require',
        'number'   => 'require',
//        'grade_id'       =>'require',
    ];

    protected $edit_message  =   [
        'id.require'=>'缺失的参数',
        'product_id.require' => '所属产品不能为空',
        'code.require' => '产品批号并不能为空',
        'company_id.require' => '所属企业不能为空',
//        'grade_id.require' => '请选择产品等级',
        'number.require' => '批次数量不能为空',
    ];
    public function edit_vali($data)
    {

        try {
            validate($this->edit_rule,$this->edit_message)->check($data);
//            $info = $this->where('code',$data['code'])->where('status',1)->find();
//            if($info['id'] !== $data['id']){
//                return re_error('批号已存在');
//            }
            if($data['wdm_open']  && $data['wdm_end']){
                if($data['wdm_open'] > $data['wdm_end']){
                    return re_error('微点码区间不正确');
                }
                $wdm_open_info = $this->where('id','<>',$data['id'])->where('wdm_end','>',$data['wdm_open'])->whereOr('wdm_end','=',$data['wdm_open'])->find();

                if($wdm_open_info){
                    return re_error('微点码开始点'.$data['wdm_open'].'已被占用，请选用其他区间');
                }

//                $info = $this->find($data['id']);
//
//                if($info['traceability']){
//                    return re_error('微点码已下发，不可修改。');
//                }
            }

        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    protected $edit_status_rule =   [
        'id'   => 'require',
        'product_id'   => 'require',
        'status'   => 'require'
    ];

    protected $edit_status_message  =   [
        'id.require'=>'缺失',
        'product_id.require'=>'缺失的产品',
        'status.require' => '状态不能为空',
    ];
    public function edit_status_vali($data)
    {

        try {

            validate($this->edit_status_rule,$this->edit_status_message)->check($data);


        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function editStatus($data)
    {

        $model = Batch::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField([
                'status'
            ])->save($data);
            $model->commit();
            return re_success("操作成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    public function edit($data)
    {
        $model = Batch::find($data['id']);

        $model->startTrans();
        try {

            $model->allowField([
                'name',
                'time',
                'user_id',
                'product_id',
                'traceability',
                'status',
                'content',
                'grade_id',
                'code',
                'number',
                'company_id',
                'wdm_end',
                'wdm_open'
            ])->save($data);
            $model->commit();
            return re_success("修改成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $dele_rule =   [
        'id'   => 'require',
        'product_id'=>'require',
        'company_id'=>'require',
    ];

    protected $dele_message  =   [
        'id.require'=>'缺失的参数',
        'product_id.require'=>'缺失的产品',
        'company_id.require'=>'缺失的企业',
    ];
    public function dele_vali($data)
    {

        try {

            validate($this->dele_rule,$this->dele_message)->check($data);
            $info = $this->where('company_id',$data['company_id'])->where('product_id',$data['product_id'])->find($data['id']);
            if($info['product_id'] !== $data['product_id']){
                return re_warning('已记录非法删除他人产品');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function dele($id)
    {

        $model = Batch::find($id);

        $model->startTrans();
        try {
            $model->delete();
            $model->commit();
            return re_success("删除成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
}